Okay, so welcome to the first project in this section of the course.
And in this project we're going to be creating some traffic lights um like this here.
So obviously this is being in the scripting section.
We're not interested in um building so much now.
We're interested in the coding of it.
Um, so, um, that's what we're going to be doing today.
Now, I've already set up, um, this the traffic lights here.
So if you're wanting to follow along, you'll need to replicate this yourself.
Um, or if you're wanting to use this as guidance, you can, um, go ahead and do that.
But, um, yeah, if you're wanting to follow along with this, um, I'll first outline how I've set
this up or the names of everything is obviously very important when it comes down to referencing.
Then I'm going to go through and, um, start coding this now.
The projects in the last lecture were usually around half, half an hour each.
I do think this will be a little bit shorter.
Um, but I guess we'll see as I'm recording this.
Okay, so, um, I've titled this intersection inside here I have lights one, two, three, four and
also the road itself.
So the road is completely separate from all the traffic lights.
Um, the inside, each light is the same as same for all of them.
I have, um, one model for the actual light itself, and then the rest of it is like the pillar and
stuff for it.
Inside there there's a bunch of parts, but, um, the most important ones is red, yellow and green.
Okay.
Um, which are the key ones I've also placed inside of all of these a spotlight.
Okay.
Um, we're going to use spotlights for this, um, as well.
So a lot of people kind of think, um, the first intention would be to change the color, um, which
in, in real life, the color doesn't change.
It's just a light that comes on.
And I think using a light makes it look a lot cooler.
It's also why I've changed the background tonight so you can see the lights, um, a bit more obviously.
Um, but um, yeah.
So if you're going to follow along with this, however, um, you want to replicate this yourself.
Okay.
You can doesn't matter.
Um, what model you use as long as it's been set up in the same way.
So, um, called intersection, you've got the four lights and the road with the exact naming as I've
used here.
Inside each one, you've got the light model, and inside the light model you've you've got a red,
yellow and green part with, um, a spotlight inside of it.
It's just called spotlight.
Okay.
You can feel free to change those around, but if you do change them, remember that the code I'm going
to be writing will be slightly different for you.
Okay, cool.
So I'm going to add a script into here and I'm going to have a go at um, well start to start to code
this.
So um, I like giving, uh, my script a title.
So I'm going to call this traffic light intersection in script.
So if I, um, because this script was a part of a much larger game, um, it might take a couple months
for me to make that game, and then I'll check back on it.
Back at it two, three months later.
I'm gonna have no idea what this script does.
So that's why I need to give it a good name and also give it a short little description at the top so
you're aware what it does.
Um.
Intersection script.
Perfect.
Let's make our variables.
So again let's define, um, everything that we need here.
So the variables I'm going to create here, um, I'm not going to go and um, make a variable for the
red, yellow and green parts for every single light.
That would be 12 variables, which is a lot.
Um, and I think it's particularly needed.
So I'm going to make a variable for each of the lights.
Um, but you can feel free to change that as well if you'd like.
So I'm going to start off by referencing the intersection.
Local intersection is equal to well the scripts parent here.
Okay.
Um, I'm then going to create um, my variables for the lights.
So local light one and I'm using um, the correct case for this.
I'm making sure this is a capital.
So it looks a lot looks a lot more readable is equal to intersection dot light one dot light okay.
Light one is the actual, uh, is the entire light and then light is.
Remember, the, um, bit actually contains, um, all of the colors and stuff inside of it, and I
could write that out again, or I could just duplicate it and, um, change all these as well.
To three.
For.
To.
Three.
Cool.
Perfect.
Now, what will make your life a lot easier?
When you are coding is testing as you go along.
Don't write your entire code and then try to run it for the first time and it doesn't work because you
then got no idea what the issue with it is.
It's so much easier to fix issues with your code, um, if you're doing it step by step.
So I made these variables here.
So what I could quickly do is just run the game.
And I noticed I get no errors coming up in the output.
Now, if I had tried to, um, reference something that didn't exist.
Um, so perhaps I spell something some one of these wrong or something like that, it would outputted
an error in the output.
So good idea.
Make sure everything's okay before you go ahead and start.
Um, that's a massive bit of advice I can give is just to keep testing as you're going along, make
sure every new kind of section of your code works before you move on to the next section.
Okay.
So, uh, the next bit is optional, but I'm going to do it, um, in this project here which is starting
values.
Now, um, this basically will um, set up the traffic lights, um, when you first start the game.
So make sure, um, the lights that are supposed to be on are on and the ones supposed to be off are
off and so on.
Okay.
Um, you don't have to do this.
Um, you could do this yourself.
But my reasoning behind it is that it means that everything, no matter what you do, if even if you
even if three, if three, four months down the line, you decide that you want to change some things
around and stuff.
Um, that will not affect how the traffic lights work.
Um, so I always like, um, to make sure everything is as it should be before I go ahead and start
coding, which is a really good bit of advice as well for that.
Um, this is pretty boring.
We're going to go through and set, um, all of the lights to be false.
So none of our lights are on.
And um, so yeah, that would be the point light.
And I also want to set to the actual parts transparency to be lower.
So like this, this green part here, for example, the decrease the transparency down a bit.
Um, so that the parts less obvious, less visible.
Okay.
Um, so that will be just going light.
One let's do a spotlight first.
So red dot spotlight dot enabled is the um property which changes whether the light on or off for the
spotlight.
So that equal to false okay.
And then light one dot red dot transparency I'm going to set the um off transparency to be 0.4.
Okay.
Um, now one thing we could do here, uh, which would be pretty cool, is make a variable called off
transparency and actually make this equal to off transparency and set this variable here to equal 0.4.
So instead of using um, this value here I'm referencing this variable which um, has its value there.
Now the um what I did earlier is known as hard coding.
Okay.
And this is where you, um, give something values, um, not as a variable, but just as the actual
value itself.
Now this is fine for smaller things, but we're going to repeat that a lot of times.
Let's say later down the line I want to change that to be 0.5.
I'll have to go through and change all those to be 0.5.
Um, now you could um, there's a replace up here, which you could use.
But then again, what if there's value if, if there's um, 0.4, um, in here that we don't want to
change.
It brings up all those kind of issues so, so much easier just to, um, make a variable for it so you
can nice and easily change it later on.
Just makes your life, um, so much easier.
We're then going to repeat this for the yellow and green.
So, um, set this to yellow.
Set this to yellow.
Set this to green.
And set that to green two.
And there we go.
That's our first light completed.
Um, so again, let's just give this a quick run.
Um, the first light you can tell is this one here, and that's how it's going to be set up when everything's
off.
Looks quite nice.
Um, whereas these ones are on they.
Yeah.
Because this one here, I've done my transparency.
These ones don't have a transparency.
And you can see the difference of that.
It's quite difficult to tell.
It looks like a screen almost even though it's not.
Um, just a nice cool feature to add in there.
We are going to have to go through here though.
Duplicate it four times.
And change all those values.
Okay.
Um, now, one thing with coding is that, um, there's always an easier way to do something.
Okay.
This is a very tedious, um, process to go through.
And there's lots of opportunities for, um, errors and things like this while doing things like that
while doing this.
Okay.
This is by no means the most efficient way to do it.
Why am I doing it?
Um, well, I know, I know a better way.
The the better, more efficient way to do this.
But you don't just yet.
Okay, so I'm going to have to.
I'll teach you this way.
Um, as this is the only way to do this, but, um, as you, um, as you learn more and more topics
and you make more and more advanced code, you'll kind of realize you're sorry.
Your actual code itself will become more efficient.
Um, you'll get what you want to do, um, done in less lines than you would do at the start, for example.
Um, so it will come, but, um, for now, unfortunately, um, things might be a bit more tedious
and, um, a bit longer to write, but, um, don't worry, as you progress through this course, um,
you'll become a lot more efficient at writing this kind of stuff.
Okay.
Perfect.
So from here we're going to start making the actual sequence for this.
Um, so let's just label this light sequence.
Okay.
So this is where we could make this simple or we could make this difficult.
Okay.
Um, now, I think traffic lights work, um, differently in different countries.
Um, some countries even have, um, different colors.
So if wherever your you're from, your traffic lights aren't red, yellow and green.
Um, that's cool.
But, um, we'll be using red, yellow and green here as well.
But, um, I'm from the UK, so where your traffic lights work in the UK is, um, d I'll do it over
here.
So yeah, the red light turns on when it's red.
Obviously.
Um, we have an amber light, which, um, is kind of like, get ready either to stop or get ready to
go.
So if it's, um, get ready to go, the red and the amber will display at the same time, and then green
will display just by itself afterwards when it's getting when you're getting ready to stop, only the
amber light shows.
And then when you're stopping again, only the red light shows.
Okay, so that's the kind of process we're going to go through here.
Now you could just do a red then just do the amber.
Just do the yellow.
Sorry.
And then just do the green.
Um either way works fine.
But um, that would be a whole lot less code.
And, um, I want to kind of, um, push you a little bit more.
Um, so you should be able to, like, completely.
Fine.
It's the same kind of code.
It's just a bit, um, extra.
Really.
Okay.
Um, cool.
So, um, we're going to use a repeat loop here, and we're going to make this a repeat until false,
because of course, this is going to be, um, repeating forever.
There'd be no time where the traffic lights would stop.
I guess you could say they should stop when the traffic light no longer exists.
If it's if it's planning to go anywhere in the future.
But for a simple thing like this, um, using a repeat until false is completely fine.
It's not always the most efficient thing to do.
Uh, repeat until false for, um, quite a few things, but for this example, it would be completely
fine.
Okay.
So, um, to start off, I am going to set the I'm going to start looking at like one example.
So, um, how I want this to work is an intersection.
So, um, if this light is green, then this light should also be green.
And if those two are green, then this one and this one should be red.
Okay.
Um, that's how we're going to kind of do this.
So to start off with we need um, the green light to show we want it to be green for light one and two
before um, we do anything else.
So light one dot green dot spotlight dot enabled is equal to true this time.
And then light one dot green dot spotlight dot enabled.
Um, sorry.
Um transparency error is equal to zero.
Now I guess we could make a on transparency value, but um, I think it's going to be zero no matter
what anyway.
So we'll, we'll leave it as zero for now.
But, um, that might be a more efficient way to do it.
Um, I'll do it actually.
Um, local on, on transparency is equal to zero.
Making things more efficient.
On transparency.
Cool.
And then, um, we can do this for light two as well.
So, um, copy this code.
Line.
Light two.
Um, so both of those green lights are showing.
Okay.
Now, um, this is where we're kind of going to be doing, um, just copying this code.
Really.
Um, that's that's all the new code you're really going to write here?
Um, it now is going to be using some wait functions and just copying this for green and for, um,
yellow as well.
Okay.
So um, once that's green I'm going to give um, all the cars and stuff 20s before um, I then turn
the yellow light on.
Okay.
Um, so when that occurs, we want the green light, um, to be hidden.
So set this to false.
Set this to false and set this to be the off transparency.
And that's the off transparency.
Okay.
And at the same time I want the yellow light to show as well okay.
So let's copy this.
Oh no copy this one actually because this one's turning it on.
Yellow.
Yellow is actually called amber in the UK, but I think in most places it's called yellow, so I've
decided to use yellow instead for this.
Um.
True on.
True on cool.
Um, the yellow light is going to just display itself for three seconds.
That's when I'm going to get the, the, um, red to show after that.
Okay.
So, um, we're going to set the, um, yellow light to be, um, off here.
So only shows three seconds and then the red light shows itself.
Cool.
And, um, we then want the red light to show.
So let's copy this and change this to red.
I said it was going to be a lot of copy and paste.
Um, again, we're using a lot of copy and paste here, a lot of codes getting repeated.
So there's going to be more efficient ways to do this.
But um, again, as we go through this, you'll, you'll, you'll learn these, uh, more efficient
ways and your code will become much smaller and much more efficient.
Cool.
So we've got, um, that showing.
Um, that's the first light completely sorted.
Really.
So let's quickly have a look at this.
Um, we want to make a quick wait here three seconds so you can actually see this.
Otherwise it would do this but it would immediately go back to the start again.
So light one is here.
So let's quickly um run this and take a look.
So it's green probably for about 20s.
Well we'll be 20s.
Same ones happening over beside as well.
I don't like this actually, so you can see them both working.
And hopefully soon it will change.
Yeah.
Perfect.
That's exactly what we want.
Cool.
Um.
And yeah, it messes up a bit afterwards.
Um, because we it hasn't been set up 100% properly yet.
We need to set up the other lights and stuff, but for now, it works.
That works completely fine.
Perfect.
Um, now we want to have a go at the other lights now.
Okay.
Um.
So, um.
Right.
So this at this point we want to turn the yellow light on for light three and light four.
But the red light isn't on yet.
So we need to turn the red light on up here.
So the first thing we should do is set this to green, and then set light three and four to be false.
Um.
Or to be red.
Sorry.
Red dot spotlight.
Dot enabled is equal to true.
Light three dot red dot transparency is equal to on transparency.
Repeat this for light four.
Perfect.
Uh, we can then go down back to here.
And this is where we want the yellow light to show.
We don't want to get rid of the red light because, as I explained earlier, um, when you're getting
ready to go, the red and yellow lights both display.
Okay, so let's copy copy this code actually, because this reference is like three and four.
Um.
Down here.
And we want to change these to be yellow.
So the red and yellow should both display at the same time.
Perfect.
And after that the yellow, yellow and red display for three seconds.
And then we want to change all those values.
So here we want to turn the red and yellow lights off and then turn the green light on.
So, um, let's reference the red.
See, I did say this is going to be a lot of copy and paste, um, which it is.
But um, finding a topic to, to um, have a go at now is quite difficult because we've only really
just started scripting.
Um, you do have to know a lot of topics in order to start making proper things, you know?
Um, but, um, as we go along in the course, you'll be able to make, um, much more, um, be able
to write a lot, a lot more code, basically.
It's very tricky to figure something out.
Um, if you have a go at, at this stage, but, um, the, um, all the basics and stuff is behind
us and, um, the following lectures and stuff is, are really going to, um, open up your doors when
it comes to scripting.
Um, so you'll be looking at learning a learning not not learning more, but, um, the code you're
using will allow you to do more kind of the basics.
You just have to know.
Um, but after that, the stuff you're learning, you can really apply into a lot of places.
Okay.
Um.
So red light off.
Yellow.
Also off.
And then the green light on as well.
Perfect.
And let's set this to be.
This needs to be on.
So let's set all this to true.
True.
On.
Perfect.
This will be on the 20s just like the other one.
And then after that we want to set the green light off.
So let's do that.
False.
Cross of.
Of, um, with the green light off.
We want the yellow to be on.
Um, so let's copy this one and change that to yellow.
Okay.
And, um, once the yellow light is on, um, right.
So green and the yellow.
Yeah.
Yellow lights on.
Wait three seconds and then turn the yellow light off.
And then the red light on the yellow is quite annoying actually.
It's only turns on for about three seconds, but, um, it's a lot of code to write.
Um, it's no much more.
It's no much.
It's no more code.
Um, making this yellow light then is the green light or the red light, even though those lights display
a lot more of the time.
Um, so no, sorry.
Yellow will be yellow will be turning off here.
Of transparency options.
Bouncy.
Perfect.
Um, yeah.
We need red light on as well.
Cool.
Let's, um.
Once the red light is on, we're going to want to wait three seconds to just, um, give a give the
cars that did pass time before we set the other cars, um, green lights on.
Then we want to set light one and, um.
Light two.
Yellow.
Yellow light to be on.
So let's copy something from there.
Um, this.
But just turning it on.
True.
On.
Perfect.
Um, and then from that point.
All we will need to do is, um.
Right.
So.
We have.
Yes.
A light one is now showing yellow.
So we then want to wait three seconds while the yellow lights on turn the red light off, and then the
green light will display right at the top here.
So um, that should be the kind of whole sequence finished.
So, um, not the most simple thing to code, um, is a lot of, um, a bit a little bit confusing.
Um, but part of the reason why it's so confusing is because we're having to write so much code.
I mean, this is 148 lines long.
Um, there are a lot more efficient ways to do this, but of course we will get to those later on in
the course.
It's not about making efficient code at the moment.
It's just about making code work, you know?
Um.
But yeah, so that should work completely fine.
Let's run this.
No initial errors.
And, um, I'm just going to show these two lights.
There's a bit of a bit of example, um, while I talk through, um, just to finish this off.
So.
Yeah, um, as I was saying, this is quite difficult one to think of an example for.
Um, because while you've done a lot of lectures in scripting, there still isn't too much that you
can do just yet.
It's not like building where, um, you can learn the first few things and was like, okay, go ahead
and make a house.
Um, it takes a long time to be able to start to make proper things.
So, um, it can be difficult learning because there's lots you need to know for before you can actually
see all your progress.
But, um.
I guess it's kind of like the halfway point.
Um, and once you and once you get the other half completed, you're then able to make, um, the majority
of things.
Really.
Um, so.
Yeah.
And you can see your traffic lights, um, working on pretty nicely, and you can see how much better
it looks, um, with the lights on.
So I'm pretty happy with that.
Um, perfect.
Cool.
So, um, I hope you've enjoyed this.
If you've if you've been following along or if you've been users, using this as a bit of example is
quite confusing.
Um, I could have made it a lot easier for myself, um, by getting not using the point lights, um,
and not including a yellow light, for example.
But, um, I wanted to I wanted to do that just to show a good example for this.
But, um, just to finish this off, this is no, this is by no means efficient code coding at all,
but it's just practice and, um, stay with it.
And, um, in a, in only a few lectures time, you'll be able to find out, uh, much more efficient
ways of writing this, but I've shown you some good coding practices, um, with, um, using these
to avoid, um, hard coding these values.
And, um, yeah, I hope you enjoy going through the rest.
And, um, I will speak to you in the next lecture.